package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBSkillDependency {
    public static final String TABLE_NAME = "GD_SKILL_DEPEND";
    public static final String FIELD_ID = "SKILL_ID";
    private static final int ROW_SKILL_ID = 1;
    private static final int ROW_DEP_SKILL_ID = 2;
    private String skillID;
    private String depSkillID;

    public DBSkillDependency() {
    }

    public DBSkillDependency(final String skillID, final String depSkillID) {
        this.skillID = skillID;
        this.depSkillID = depSkillID;
    }

    public String getDependentSkillID() {
        return this.depSkillID;
    }

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GD_SKILL_DEPEND";
        final String createTable = "CREATE TABLE GD_SKILL_DEPEND (SKILL_ID  VARCHAR(256) NOT NULL, DEP_SKILL_ID VARCHAR(256), PRIMARY KEY (SKILL_ID, DEP_SKILL_ID))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GD_SKILL_DEPEND")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            final Object[] args = {"GD_SKILL_DEPEND"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
            GDMsgLogger.addError(msg);
            throw ex;
        }
    }

    public static void delete(final Connection conn, final String skillID) throws SQLException {
        final String deleteEntry = "DELETE FROM GD_SKILL_DEPEND WHERE SKILL_ID = ?";
        try (final PreparedStatement ps = conn.prepareStatement(deleteEntry)) {
            ps.setString(1, skillID);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static void insert(final Connection conn, final String skillID, final List<DBSkillDependency> dependencies) throws SQLException {
        final String insert = "INSERT INTO GD_SKILL_DEPEND VALUES (?,?)";
        if (dependencies == null) {
            return;
        }
        if (dependencies.isEmpty()) {
            return;
        }
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final DBSkillDependency dep : dependencies) {
                ps.setString(1, skillID);
                ps.setString(2, dep.depSkillID);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
        }
    }

    public static List<DBSkillDependency> getBySkillID(final String skillID) {
        if (skillID == null) {
            return null;
        }
        List<DBSkillDependency> list = new LinkedList<DBSkillDependency>();
        final String command = "SELECT * FROM GD_SKILL_DEPEND WHERE SKILL_ID = ?";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            ps.setString(1, skillID);
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {skillID, "GD_SKILL_DEPEND"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBSkillDependency> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<DBSkillDependency> list = new LinkedList<DBSkillDependency>();
        final Blob blob = null;
        while (rs.next()) {
            final DBSkillDependency dep = new DBSkillDependency();
            dep.skillID = rs.getString(1);
            dep.depSkillID = rs.getString(2);
            list.add(dep);
        }
        return list;
    }
}
